
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class EncripcionOK {
    public static void main ( String args[ ] ) throws IOException, IllegalBlockSizeException {

        // Generar la llave
        try
        {
        KeyGenerator keygen = KeyGenerator.getInstance("DES");
        SecretKey desKey = keygen.generateKey();
        // Iniciar un objeto para la encr / desencr
        Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

        // Leer y encriptar un dato
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
          //  String pwd = stdIn.readLine();
            byte[] cleartext =  ConvertirArchivo.fileToByte("JXTA.pdf");
            String s1 = new String(cleartext);
      //      System.out.println("password original:" + s1);
            desCipher.init(Cipher.ENCRYPT_MODE, desKey);
            byte[] ciphertext = desCipher.doFinal(cleartext);
//            String s2 = new String(ciphertext);
//            System.out.println("password encriptado:" + s2);
       
            ConvertirArchivo.byteToFile("JXTA.pdf", ciphertext);

            // Ahora desencriptar
            desCipher.init(Cipher.DECRYPT_MODE, desKey);
            byte[] cleartext1 = desCipher.doFinal(ciphertext);

           ConvertirArchivo.byteToFile("JXTA.pdf", cleartext1);

        } catch (java.security.NoSuchAlgorithmException e1) {
        } catch (java.security.InvalidKeyException e2) {
        } catch (IllegalBlockSizeException e3) {
        } catch (javax.crypto.NoSuchPaddingException e4) {
        } catch (javax.crypto.BadPaddingException e5) {
        } catch (IOException e) {
        }
    }
}
